package com.zenith.user.service.impl;

import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.zenith.user.mapper.PermissionMapper;
import com.zenith.user.po.domain.Permission;
import com.zenith.user.po.domain.Role;
import com.zenith.user.po.domain.RolePermission;
import com.zenith.user.po.dto.PermissionDTO;
import com.zenith.user.service.PermissionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zenith.common.result.PageResult;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zenith.user.service.RolePermissionService;
import com.zenith.user.service.RoleService;
import com.zenith.user.service.UserRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Set;

/**
 * 权限 业务实现
 *
 * @author makejava
 * @since 2025-08-15
 */
@Service
public class PermissionServiceImpl extends ServiceImpl<PermissionMapper, Permission> implements PermissionService {

    @Autowired
    private UserRoleService userRoleService;

    @Autowired
    private RoleService roleService;

    @Autowired
    private RolePermissionService rolePermissionService;
    
    @Autowired
    private PermissionService permissionService;

    @Override
    public PageResult<Permission> getPermissionPage(PermissionDTO query) {
        // 创建分页对象
        Page<Permission> page = new Page<>(query.getPageNum() != null ? query.getPageNum() : 1, 
                                          query.getPageSize() != null ? query.getPageSize() : 10);
        // 执行分页查询
        Page<Permission> pageResult = lambdaQuery().page(page);
        // 封装返回结果
        return PageResult.ofPage(page.getCurrent(), page.getSize(), pageResult.getTotal(), pageResult.getRecords());
    }

    @Override
    public Boolean getPermissionIds(Long userId, String requiredPermission) {
        //查询用户绑定的角色
        Set<String> userRoleIds = userRoleService.getUserRoleIds(userId);
        //查询角色绑定的权限
        Set<String> permissionIds = userRoleService.getUserPermissionIds(userId);
        //判断用户的角色是否拥有此权限
        return userRoleIds.contains(requiredPermission);
    }

    @Override
    public List<Permission> selectPermissionsByRole(String role) {
        LambdaQueryChainWrapper<Role> query = new LambdaQueryChainWrapper<>(roleService.getBaseMapper());
        Role roleEntity = query.eq(Role::getRoleName, role).one();
        
        if (roleEntity == null) {
            log.error("角色不存在");
            return List.of();
        }

        LambdaQueryChainWrapper<RolePermission> queryPermission = new LambdaQueryChainWrapper<>(rolePermissionService.getBaseMapper());
        List<RolePermission> rolePermissions = queryPermission.eq(RolePermission::getRoleId, roleEntity.getId()).list();
        List<Permission> permissions = permissionService.listByIds(rolePermissions.stream().map(RolePermission::getPermissionId).toList());
        return permissions;
    }
}